/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cageballcommon.io.impl;

import cageballcommon.io.interfaces.CageballIO;
import cageballcommon.exceptions.CageballIOException;
import cageballcommon.exceptions.CageballParseException;
import cageballcommon.messages.Action;
import cageballcommon.messages.InitTeam;
import cageballcommon.messages.State;
import java.io.IOException;
import java.net.Socket;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kanonas
 */
public class CageballSocketIO implements CageballIO {

    private CageballSocketReader reader;
    private CageballSocketWriter writer;
    private final Socket socket;

    public CageballSocketIO(Socket socket) throws IOException {
        this.socket = socket;
        reader = new CageballSocketReader(socket);
        writer = new CageballSocketWriter(socket);
    }

    public State readState() throws CageballIOException, CageballParseException {
        return reader.readState();
    }

    public Action readAction() throws CageballIOException, CageballParseException {
        return reader.readAction();
    }

    public InitTeam readInitTeam() throws CageballIOException, CageballParseException {
        return reader.readInitTeam();
    }

    public Properties readProperties() throws CageballIOException, CageballParseException {
        return reader.readProperties();
    }

    public void close() throws CageballIOException {
        try {
            writer.closeStream();
            reader.closeStream();
            socket.close();
        } catch (IOException ex) {
           throw new CageballIOException(ex.getMessage());
        }
    }

    public void writeState(State state) throws CageballIOException {
        writer.writeState(state);
    }

    public void writeAction(Action action) throws CageballIOException {
        writer.writeAction(action);
    }

    public void writeInitTeam(InitTeam initTeam) throws CageballIOException {
        writer.writeInitTeam(initTeam);
    }

    public void writeProperties(Properties properties) throws CageballIOException {
        writer.writeProperties(properties);
    }
}
